import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/finished_in_scan_odoo_api.dart';
import 'package:flutter_admin/api/odoo/stock_move_line_odoo_api.dart';
import 'package:flutter_admin/api/odoo/stock_quant_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/finish_in.dart';
import 'package:flutter_admin/models/odoo/stock_move_line.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/cry_dialog.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input_num.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/utils/http_util.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/utils/eventbus.dart';
import 'package:qrscan/qrscan.dart' as scanner;
import 'package:quiver/strings.dart';

class HWWarehousing extends StatefulWidget {
  HWWarehousing({Key? key,this.stockQuant}) : super(key: key);

  StockQuant? stockQuant;

  @override
  _HWWarehousingState createState() => _HWWarehousingState();
}

class _HWWarehousingState extends State<HWWarehousing> {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  StockQuant stockQuant = StockQuant();
  List<SelectOptionVO> locationList = [];
  @override
  void initState() {
    super.initState();
    stockQuant = widget.stockQuant!;
    _loadSelectVO();
  }

  @override
  void dispose() {
    super.dispose();
  }

  _loadSelectVO() async {
    locationList = await DynamicDataApi.getModelSelect(
        active: false,
        model: "stock.location",
        name: "complete_name",
        key: 'father_locaiton',
        val: null,
        key2: 'shelves_name',
        val2: '1'
    );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '销售单号',
            enable: false,
            value: stockQuant.saleorder,
          ),
          CryInput(
            label: '生产单号',
            enable: false,
            value: stockQuant.productorderno,
          ),
          CryInput(
            label: '产品名称',
            enable: false,
            value: stockQuant.productName,
          ),
          CryInput(
            label: '产品编码',
            enable: false,
            value: stockQuant.productCode,
          ),
          CryInput(
            label: '规格型号',
            enable: false,
            value: stockQuant.productDes,
          ),
          CryInputNum(
            label: '数量',
            enable: false,
            value: stockQuant.quantity,
          ),
          CryInput(
            label: '单位',
            enable: false,
            value: stockQuant.productUomName,
          ),
          CryInput(
            label: '备注',
            enable: false,
            value: stockQuant.remark,
          ),
          CrySelect(
            label: '库房',
            dataList: locationList,
            value: stockQuant.locationId,
            onChange: (v){
              stockQuant.locationId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
        ],
      ),
    );

    var footerBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            StockQuantOdooApi.getCountNum(int.parse(stockQuant.id!)).then((ResponseBodyApi res) {
              if (!res.success!) {
                return;
              }
              cryConfirm(context, '当前包装进度${res.data},是否要完成入库', (context) async {
                StockQuantOdooApi.HWStorage({'id': int.parse(stockQuant.id!),'location_id': int.parse(stockQuant.locationId!)}).then((ResponseBodyApi res) {
                  if (!res.success!) {
                    return;
                  }
                  Navigator.pop(context, true);
                  CryUtils.message(S.of(context).saved);
                });
              });
            });

          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      key: scaffoldStateKey,
      appBar: AppBar(
        title: Text("入库确认"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: footerBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
  }


// _loadData() async {
//   print(finishedInScan.productorderno);
//   var finishedInScanToOdoo = [
//     if (finishedInScan.productorderno != null &&
//         finishedInScan.productorderno != '')
//       ['name', '=', finishedInScan.productorderno],
//     if (finishedInScan.productCode != null &&
//         finishedInScan.productCode != '')
//       [
//         'product_id.product_tmpl_id.default_code',
//         '=',
//         finishedInScan.productCode
//       ],
//   ];
//   ResponseBodyApi responseBodyApi = await FinishedInScanOdooApi.page(
//       data: {"domainList": finishedInScanToOdoo});
//   if (responseBodyApi.success!) {
//     tableList = responseBodyApi.data;
//     setState(() {});
//   }
// }

}
